﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Newtonsoft.Json;
using TaxiReviews.Models;

namespace TaxiReviews.Controllers
{
    public class DriverController : ApiController
    {
     
        readonly TaxiReviewsEntities context = new TaxiReviewsEntities();

        //
        // GET: api/Drivers/
        public IEnumerable<Driver> Get()
        {
            return context.fn_Drivers_Select();
        }

        public IEnumerable<Driver> GetDriver(int id)
        {
            return context.fn_Drivers_Select();
        }

        [System.Web.Http.HttpPost]
        public HttpResponseMessage Create([FromBody] Driver oDriver)
        {
            try
            {
                //bad request passed
                if (oDriver ==null)
                {
                    return Request.CreateErrorResponse(HttpStatusCode.BadRequest, "invalid driver supplied");
                }

                //insert the driver
                var result = context.fn_Drivers_Insert(oDriver.Name, oDriver.Email, oDriver.Telephone);
                foreach (Drivers_INSERT singleResult in result)
                {
                    oDriver.IDDriver = (long)singleResult.IDDriver;
                    oDriver.IDUser = (long)singleResult.IDUser;
                }
                
                //generate the response and return
                var successresponse = Request.CreateResponse<Driver>(HttpStatusCode.Created, oDriver);
                string uri = Url.Link("DefaultApi", new { id = oDriver.IDDriver });
                successresponse.Headers.Location= new Uri(uri);
                return successresponse;

            }
            catch (Exception e)
            {
                //return the exception
                var errorResponse = Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e);
                return errorResponse;
            }
        }


        public void DeleteDriver(int id)
        {
            var result = context.fn_Drivers_Delete(id);
            //repository.Remove(id);
        } 
     

        //test
        //public HttpResponseMessage GetQuota(int id){} 
        //[System.Web.Http.HttpPost] public HttpResponseMessage SetQuota(int id, DocType doc, QuotaType quota, int value){} 
        //[System.Web.Http.HttpPost] public HttpResponseMessage Increment{} [System.Web.Http.HttpPost] 
        //public HttpResponseMessage Decrement{} } 
        //end test
    }
}